let util = require('../../utils/util');

let requestDomain = util.config('requestDomain');

let that;

Page({

  data: {
    orderId: '',
    orderDate: '',
    payMode: '',
    orderStore: '',
    orderTableNum: '',
    orderDone: false,
    userPayed: false,
    totalPrice: 0,
    foodPhotoRequestUrl: ''
  },

  onLoad: function (params) {
    that = this;
    if(!params.orderId) {
        util.showError('错误提示', '错误的订单ID');
    }
    else {
        //设置请求食物图片的字符串
        that.setData({ foodPhotoRequestUrl: util.config('foodPhotoRequestUrl').replace('{storeId}', util.globalData('storeId')) });
        that.setData({ orderId: params.orderId });
        that.getOrderDetail(params.orderId);
    }
  },

  getOrderDetail: function(orderId) {
      wx.showNavigationBarLoading();
      wx.request({
          url: requestDomain + 'order/getOrderDetail',
          method: 'POST',
          data: {
            storeId: util.globalData('storeId'),
            _3rdSession: util.globalData('_3rdSession'),
            orderId: orderId
          },
          success: function(result) {
              wx.hideNavigationBarLoading();
            if(util.checkSuccess(result) && result.data && result.data.data) {
                let {orderData, goodsList} = result.data.data;
                let date = new Date(orderData.timestamp * 1000);
                that.setData({
                    orderId: orderData.orderId,
                    orderDate: [(date.getFullYear() > 9 ? date.getFullYear() : '0' + date.getFullYear()), (date.getMonth() > 9 ? date.getMonth() : '0' + date.getMonth()), (date.getDate() > 9 ? date.getDate() : '0' + date.getDate())].join('-') + ' ' + [date.getHours() > 9 ? date.getHours() : '0' + date.getHours(), date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(), date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds()].join(':'),
                    payMode: orderData.payMode,
                    orderStore: orderData.storeName,
                    orderTableNum: orderData.tableNum,
                    orderDone: orderData.orderDone,
                    userPayed: orderData.userPayed
                });
                let _goodsList = [];
                let totalPrice = 0;
                console.log(goodsList);
                for(let goodsIndex in goodsList) {
                    let {foodId, goodsId, count, goodsCount} = goodsList[goodsIndex];
                    for (let _goodsIndex in goodsList) {
                        if ((foodId && foodId == goodsList[_goodsIndex].foodId) || (goodsId && goodsId == goodsList[_goodsIndex].goodsId)) {
                            let {foodName, goodsName, foodPhoto, foodPrice, goodsPrice} = goodsList[_goodsIndex];
                            totalPrice += (foodPrice || goodsPrice) * (count || goodsCount);
                            _goodsList.push({
                                goodsName: foodName || goodsName,
                                goodsCount: count || goodsCount,
                                foodPhoto: foodPhoto,
                                goodsPrice: foodPrice || goodsPrice
                            });
                            that.setData({
                                goodsList: _goodsList,
                                totalPrice: totalPrice
                            });
                            break;
                        }
                    }
                }
                wx.stopPullDownRefresh();
            }
            else {
                wx.stopPullDownRefresh();
                util.showError('获取详情失败', '查询订单详情时出现错误' + result.data.errMsg, result.data.errCode);
            }
          },
          fail: function() {
              wx.stopPullDownRefresh();
              wx.hideNavigationBarLoading();
              util.showError('获取详情失败', '发起获取订单详情请求失败');
          }
      });
  },

  onPullDownRefresh: function() {
      that.setData({ foodPhotoRequestUrl: util.config('foodPhotoRequestUrl').replace('{storeId}', util.globalData('storeId')) });
      that.getOrderDetail(that.data.orderId);
  }

})